//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.example.aaaatest.a2;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {
    private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final ObjectMapper OBJECT_MAPPER_FILTER_NULL = new ObjectMapper();

    public static String toJson(Object pojo) {
        try {
            return OBJECT_MAPPER.writeValueAsString(pojo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJsonWithoutException(Object pojo) {
        try {
            return OBJECT_MAPPER.writeValueAsString(pojo);
        } catch (Throwable e) {
            log.error("to json error!", e);
            return null;
        }
    }

    public static <T> T fromJson(String json, Class<T> classOfT) {
        try {
            return (T)OBJECT_MAPPER.readValue(json, classOfT);
        } catch (Exception e) {
            throw new RuntimeException("parse json error, json=" + json, e);
        }
    }

    public static <T> T fromJsonWithoutException(String json, Class<T> classOfT) {
        try {
            return (T)OBJECT_MAPPER.readValue(json, classOfT);
        } catch (Throwable e) {
            log.error("from json error!", e);
            return null;
        }
    }

    public static <T> T fromJson(String json, TypeReference<T> typeReference) {
        try {
            return (T)OBJECT_MAPPER.readValue(json, typeReference);
        } catch (Exception e) {
            throw new RuntimeException("parse json error, json=" + json, e);
        }
    }

    public static <T> T fromJsonWithoutException(String json, TypeReference<T> typeReference) {
        try {
            return (T)OBJECT_MAPPER.readValue(json, typeReference);
        } catch (Throwable e) {
            log.error("from json error!", e);
            return null;
        }
    }

    public static <T> List<T> fromJsonToList(String json, Class<T> classOfT) {
        try {
            CollectionType collectionType = OBJECT_MAPPER.getTypeFactory().constructCollectionType(ArrayList.class, classOfT);
            return (List)OBJECT_MAPPER.readValue(json, collectionType);
        } catch (Exception e) {
            throw new RuntimeException("parse json error, json=" + json, e);
        }
    }

    public static JsonNode toJsonTree(String json) {
        try {
            return OBJECT_MAPPER.readTree(json);
        } catch (Exception e) {
            throw new RuntimeException("parse json error, json=" + json, e);
        }
    }

    public static String toJsonFilterNull(Object pojo) {
        try {
            return OBJECT_MAPPER_FILTER_NULL.writeValueAsString(pojo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJsonFilterNullWithoutException(Object pojo) {
        try {
            return OBJECT_MAPPER_FILTER_NULL.writeValueAsString(pojo);
        } catch (Throwable e) {
            log.error("toJsonFilterNullWithoutException error!", e);
            return null;
        }
    }

    private JsonUtils() {
    }

    static {
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        OBJECT_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"));
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
        LocalDateTimeSerializer timeSerializer = new LocalDateTimeSerializer(formatter);
        LocalDateTimeDeserializer timeDeserializer = new LocalDateTimeDeserializer(formatter);
        javaTimeModule.addSerializer(LocalDateTime.class, timeSerializer);
        javaTimeModule.addDeserializer(LocalDateTime.class, timeDeserializer);
        OBJECT_MAPPER.registerModule(javaTimeModule);
        OBJECT_MAPPER_FILTER_NULL.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        OBJECT_MAPPER_FILTER_NULL.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        OBJECT_MAPPER_FILTER_NULL.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"));
        OBJECT_MAPPER_FILTER_NULL.setSerializationInclusion(Include.NON_NULL);
        javaTimeModule = new JavaTimeModule();
        formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
        timeSerializer = new LocalDateTimeSerializer(formatter);
        timeDeserializer = new LocalDateTimeDeserializer(formatter);
        javaTimeModule.addSerializer(LocalDateTime.class, timeSerializer);
        javaTimeModule.addDeserializer(LocalDateTime.class, timeDeserializer);
        OBJECT_MAPPER_FILTER_NULL.registerModule(javaTimeModule);
    }
}
